const express = require("express");
const http = require("http");
const apicache = require("apicache");
const path = require("path");

const app = express();
let cache = apicache.middleware;

// 跨域设置
 app.all('*', function (req, res, next) {
   if (req.path !== '/' && !req.path.includes('.')) {
     res.header('Access-Control-Allow-Credentials', true)
     // 这里获取 origin 请求头 而不是用 *
     res.header('Access-Control-Allow-Origin', req.headers['origin'] || '*')
     res.header('Access-Control-Allow-Headers', 'X-Requested-With')
     res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
     res.header('Content-Type', 'application/json;charset=utf-8')
   }
   next()
 })
const onlyStatus200 = (req, res) => res.statusCode === 200;

app.use(cache("1 second", onlyStatus200));

app.use(express.static(path.resolve(__dirname, "public")));


//手机登录
app.use("/login/cellphone", require("../router/loginCellphone"));
//分类歌单
app.use("/top/playlist", require("../router/top_playlist"));

//登录刷新
app.use("/login/refresh", require("../router/login_refresh"));

// 私人 FM
app.use("/personal_fm", require("../router/personal_fm"));

// 获取音乐 url
app.use("/music/url", require("../router/musicUrl"));

//用户动态
app.use("/user/event", require("../router/user_event"));

app.use("/user/detail", require("../router/user_detail"));

// 获取音乐详情
app.use("/song/detail", require("../router/song_detail"));

// 获取每日推荐歌曲
app.use("/recommend/songs", require("../router/recommend_songs"));

// 获取用户歌单
app.use("/user/playlist", require("../router/user_playlist"));


//精品歌单
app.use(
  "/top/playlist/highquality",
  require("../router/top_playlist_highquality")
);

const port = process.env.PORT || 3000;

app.listen(port, () => {
  console.log(`server running @${port}`);
});

module.exports = app;

